package testProjectZOP;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

/**
 * 
 * @author mrc
 * 2016-10-26
 */
public class Main{
	
	public static void main(String[] args) throws Exception{
		JarFile jarFile = new JarFile("zop-sdk-1.0.jar");
		JarEntry jarEntry = jarFile.getJarEntry("zop-sdk.properties");
		InputStream inputStream = jarFile.getInputStream(jarEntry);
		
		//修改配置文件
		Properties properties = new Properties();
		properties.load(inputStream);
		properties.setProperty("app.private_key", "tts");
		
		//暂存到文件
		OutputStream outputStream = new FileOutputStream(new File("zop-sdk.properties"));
		properties.store(outputStream, "");
		
		//从文件中读取并转化为byte数组
		InputStream  in = new FileInputStream(new File("zop-sdk.properties"));
		byte[] temp = InputStreamTOByte(in);
		System.out.println(new String(temp));
		inputStream.close();
		in.close();
		
		//修改并新增一个jar
		editJar("zop-sdk-1.0.jar" ,"zop-sdk-2.0.jar" , jarEntry.getName(), temp);
	}
	
	/**
	 * 通用修改jar方法
	 * @param file 读取的jar
	 * @param outFile 得到的jar
	 * @param EntryName 修改或新增的文件名
	 * @param data 修改或新增的内容
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void editJar(String file ,String outFile,String EntryName,byte[] data)  
	{
		try{  
			JarFile jarFile = new JarFile(file);
			TreeMap treeMap = new TreeMap();
			Enumeration enumeration = jarFile.entries();
			//读取原jar中的文件以key:文件名，value:字节数组，存储到map里面
			while(enumeration.hasMoreElements())
			{
				JarEntry jarEntry = (JarEntry)enumeration.nextElement();
				byte[] byteArray = InputStreamTOByte(jarFile.getInputStream(jarEntry));
				treeMap.put(jarEntry.getName(), byteArray);
			}
			JarOutputStream out = new JarOutputStream(new FileOutputStream(outFile));
			Set set = treeMap.entrySet();
			Iterator it = set.iterator();
			//是否新增新文件
			boolean has = false;
			while(it.hasNext())
			{  
				Map.Entry me = (Map.Entry)it.next();
				String name = (String)me.getKey();
				JarEntry jeNew = new JarEntry(name);
				out.putNextEntry(jeNew);
				byte[] b;
				//如果是当前要修改的文件，则要用传进来的参数，丢弃原来的内容
				if(name.equals(EntryName))
				{
					b = data;
					has = true;
				}
				else{
					b = (byte[])me.getValue();
				}
				out.write(b, 0, b.length);
			}
			//如果当前文件不存在于原jar文件中，则新增
			if(!has)
			{
				JarEntry jeNew = new JarEntry(EntryName);
				out.putNextEntry(jeNew);
				out.write(data, 0, data.length);
			}
			out.finish();
			out.close();
		}catch(Exception e){}
	}
	
	/**  
	* 将InputStream转换成byte数组  
	* @param in InputStream  
	* @return byte[]  
	* @throws IOException  
	*/  
	public static byte[] InputStreamTOByte(InputStream in) throws IOException{
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		byte[] buff = new byte[100];
		int rc = 0;  
		while ((rc = in.read(buff, 0, 100)) > 0) {
			swapStream.write(buff, 0, rc);
		}
		byte[] in2b = swapStream.toByteArray();
		return in2b;
	} 
}
	